<?php


class IndexController extends Controller {
    
    public function order_by()
    {
        require_once "model/DemotModel.php";
        require_once "model/DemotCategoryModel.php";
        require_once "library/external/pager.php";
        
        $config = Controller::getConfig();
        $action = "popularne";
        
        $oPager = new Vframe_Pagination();
        $oPager->limit(intval($config['demot']['perPage']));
        $demotCount = DemotModel::count(DStatus::NORMAL);
        
        $listArray = DemotModel::fetchOrderBy($oPager->start()-1, $oPager->limit(), DStatus::FILTER_TOP);
        $i = intval(count($listArray));
        $oPager->items(DemotModel::countOrderBy(DStatus::FILTER_TOP));
        
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $oPager->page($page);
        
        $oPager->PatternPage('<div class="nav"><a href="'.View::getRoot().'/index/'.$action.'/[$]"> [$] </a></div>');
        $oPager->PatternPageCurrent('<div class="nav current"><strong> [$] </strong></div>');
        //$oPager->PatternPageNavigation('<div class="nav"><a href="index.php?controller=index&action=index&page=[$]" rel="nofollow">[$$]</a></div>', array('&laquo; poprzednia', 'następna &raquo;'));
        $oPager->PatternSeparator('<span>...</span>');
        
        View::assign('pager', $oPager);
        View::assign('demotCount', $demotCount);
        View::assign('demotPerPage', intval($config['demot']['perPage']));
       
        View::assign('demots', $listArray); 
        
        self::setLayout('demot');
        self::setContentFile('parts/demot/list');
    }
    
    public function queue()
    {
        require_once "model/DemotModel.php";
        require_once "model/DemotCategoryModel.php";
        require_once "library/external/pager.php";
        
        $config = Controller::getConfig();
        $action = "kolejka";
        
        $oPager = new Vframe_Pagination();
        $oPager->limit(intval($config['demot']['perPage']));
        $demotCount = DemotModel::count(DStatus::QUEUE);
        
        $oPager->items($demotCount);
        
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $oPager->page($page);
        
        $oPager->PatternPage('<div class="nav"><a href="'.View::getRoot().'/index/'.$action.'/[$]"> [$] </a></div>');
        $oPager->PatternPageCurrent('<div class="nav current"><strong> [$] </strong></div>');
        //$oPager->PatternPageNavigation('<div class="nav"><a href="index.php?controller=index&action=index&page=[$]" rel="nofollow">[$$]</a></div>', array('&laquo; poprzednia', 'następna &raquo;'));
        $oPager->PatternSeparator('<span>...</span>');
        
        View::assign('pager', $oPager);
        View::assign('demotCount', $demotCount);
        View::assign('demotPerPage', intval($config['demot']['perPage']));
       
        View::assign('demots', DemotModel::fetchAll($oPager->start()-1, $oPager->limit(), DStatus::QUEUE));   
        
        self::setLayout('demot');
        self::setContentFile('parts/demot/list');
    }
    
    public function random()
    {
        require_once "model/DemotModel.php";
        require_once "model/DemotCategoryModel.php";
        require_once "library/external/pager.php";
        
        $config = Controller::getConfig();
        $action = "losowe";
        
        $oPager = new Vframe_Pagination();
        $oPager->limit(intval($config['demot']['perPage']));
        $demotCount = DemotModel::count(DStatus::NORMAL);
        
        $oPager->items($demotCount);
        
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $oPager->page($page);
        
        $oPager->PatternPage('<div class="nav"><a href="'.View::getRoot().'/index/'.$action.'/[$]"> [$] </a></div>');
        $oPager->PatternPageCurrent('<div class="nav current"><strong> [$] </strong></div>');
        //$oPager->PatternPageNavigation('<div class="nav"><a href="index.php?controller=index&action=index&page=[$]" rel="nofollow">[$$]</a></div>', array('&laquo; poprzednia', 'następna &raquo;'));
        $oPager->PatternSeparator('<span>...</span>');
        
        View::assign('pager', $oPager);
        View::assign('demotCount', $demotCount);
        View::assign('demotPerPage', intval($config['demot']['perPage']));
       
        View::assign('demots', DemotModel::fetchOrderBy($oPager->start()-1, $oPager->limit(), DStatus::FILTER_RANDOM));   
        
        self::setLayout('demot');
        self::setContentFile('parts/demot/list');
    }
    
    public function category()
    {
        require_once "model/DemotModel.php";
        require_once "model/DemotCategoryModel.php";
        require_once "library/external/pager.php";
        
        $config = Controller::getConfig();
        $action = "kategoria";
        
        $oPager = new Vframe_Pagination();
        $oPager->limit(intval($config['demot']['perPage']));
        
        $id_cat = isset($_REQUEST['id_cat']) ? intval($_REQUEST['id_cat']) : 0;
        
        if ($id_cat  == 0)
            $demotCount = DemotModel::count(DStatus::NORMAL);
        else        
            $demotCount = DemotModel::countByCategory($id_cat);
        
        $oPager->items($demotCount);
        
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
       
        $oPager->page($page);
   
        
        $oPager->PatternPage('<div class="nav"><a href="'.View::getRoot().'/demot/'.$action.'/'.$id_cat.'/[$]"> [$] </a></div>');
        $oPager->PatternPageCurrent('<div class="nav current"><strong> [$] </strong></div>');
        //$oPager->PatternPageNavigation('<div class="nav"><a href="index.php?controller=index&action=index&page=[$]" rel="nofollow">[$$]</a></div>', array('&laquo; poprzednia', 'następna &raquo;'));
        $oPager->PatternSeparator('<span>...</span>');
        
        View::assign('pager', $oPager);
        View::assign('demotCount', $demotCount);
        View::assign('demotPerPage', intval($config['demot']['perPage']));
       
        View::assign('demots_categories', DemotCategoryModel::fetchAll());
        
        if ($id_cat  == 0)
            View::assign('demots', DemotModel::fetchOrderBy($oPager->start()-1, $oPager->limit(), DStatus::FILTER_RANDOM));   
        else 
            View::assign('demots', DemotModel::fetchByCategory($oPager->start()-1, $oPager->limit(), $id_cat ));   
               
        self::setLayout('demot');
        self::setContentFile('parts/demot/list');
    }
    
    public function search()
    {
        require_once "model/DemotModel.php";
        require_once "model/DemotCategoryModel.php";
        require_once "library/external/pager.php";
        
        $config = Controller::getConfig();
        $action = "szukaj";
        
        $oPager = new Vframe_Pagination();
        $oPager->limit(intval($config['demot']['perPage']));
        
        $query = isset($_REQUEST['query']) ? trim($_REQUEST['query']) : "";
                 
        if ($query == "")
            $demotCount = DemotModel::count(DStatus::NORMAL)+1;
        else
            $demotCount = DemotModel::countByQuery($query);
        
        $oPager->items($demotCount);
              
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
       
        $oPager->page($page);   
        
        $oPager->PatternPage('<div class="nav"><a href="'.View::getRoot().'/demot/'.$action.'/'.$query.'/[$]"> [$] </a></div>');
        $oPager->PatternPageCurrent('<div class="nav current"><strong> [$] </strong></div>');
        //$oPager->PatternPageNavigation('<div class="nav"><a href="index.php?controller=index&action=index&page=[$]" rel="nofollow">[$$]</a></div>', array('&laquo; poprzednia', 'następna &raquo;'));
        $oPager->PatternSeparator('<span>...</span>');
        
        View::assign('pager', $oPager);
        View::assign('demotCount', $demotCount);
        View::assign('demotPerPage', intval($config['demot']['perPage']));
       
        View::assign('is_search', 1);
                
        View::assign('demots', DemotModel::fetchByQuery($oPager->start()-1, $oPager->limit(), $query));   
               
        self::setLayout('demot');
        self::setContentFile('parts/demot/list');
    }
    
    public function terms()
    {
        self::setLayout('page');
        self::setContentFile('parts/index/terms');
    }
    
    
    public function access_denied()
    {
        self::setLayout('page');
        self::setContentFile('parts/index/access-denied');
    }
    
    public function index()
    {
        require_once "model/DemotModel.php";
        require_once "model/DemotCategoryModel.php";
        require_once "library/external/pager.php";
        
        $config = Controller::getConfig();
        $action = 'index';
        
        $oPager = new Vframe_Pagination();
        $oPager->limit(intval($config['demot']['perPage']));
        $demotCount = DemotModel::count(DStatus::NORMAL)+1;
        
        $oPager->items($demotCount);
        
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $oPager->page($page);
                
        $oPager->PatternPage('<div class="nav"><a href="'.View::getRoot().'/demot/'.$action.'/'.$id_cat.'/[$]"> [$] </a></div>');
        $oPager->PatternPageCurrent('<div class="nav current"><strong> [$] </strong></div>');
        //$oPager->PatternPageNavigation('<div class="nav"><a href="index.php?controller=index&action=index&page=[$]" rel="nofollow">[$$]</a></div>', array('&laquo; poprzednia', 'następna &raquo;'));
        $oPager->PatternSeparator('<span>...</span>');
        
        View::assign('pager', $oPager);
        View::assign('demotCount', $demotCount);
        View::assign('demotPerPage', intval($config['demot']['perPage']));
       
        View::assign('demots', DemotModel::fetchAll($oPager->start()-1, $oPager->limit(), DStatus::NORMAL));   
        
        self::setLayout('demot');
        self::setContentFile('parts/demot/list');
    }
    
    public function page()
    {
        self::setLayout('page');
    }
    
    public function captcha()
    {
        $pool = '0123456789abcdefghijklmnopqrstuvwxyz';
        $img_width = 120;
        $img_height = 30;

        $str = '';
        for ($i = 0; $i < 7; $i++){
                $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
        }

        $string = $str;
        $_SESSION['captcha'] = $string;

        $im = imagecreate($img_width, $img_height);

        $bg_color        = imagecolorallocate($im,163,163,163);
        $font_color   = imagecolorallocate($im,252,252,252);
        $grid_color   = imagecolorallocate($im,31,0,0);
        $border_color = imagecolorallocate ($im, 174, 174, 174);

        imagefill($im,1,1,$bg_color);

        for($i=0; $i<1600; $i++){

                $rand1 = rand(0,$img_width);
                $rand2 = rand(0,$img_height);
                imageline($im, $rand1, $rand2, $rand1, $rand2, $grid_color);

        }

        $x = rand(5, $img_width/(7/2));

        imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);

        for($a=0; $a < 7; $a++){

                imagestring($im, 17, $x, rand(12 , $img_height/5), substr($string, $a, 1), $font_color);
                $x += (5*2); #odstęp

        }

        header("Content-type: image/gif");
        imagegif($im);
        imagedestroy($im);

    }
}

?>
